package com.acme.entity;

import com.acme.entity.util.ChangeSupportList;
import com.acme.entity.util.EntityBean;
import java.util.List;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.swing.event.ChangeListener;

/**
 *
 * @author alejandro
 */
@Entity
@Table(name = "CARRETERA", catalog = "carreteras", schema = "dbo")
@NamedQueries({
    @NamedQuery(name = "Carretera.findAll", query = "SELECT c FROM Carretera c"),
    @NamedQuery(name = "Carretera.findById", query = "SELECT c FROM Carretera c WHERE c.id = :id"),
    @NamedQuery(name = "Carretera.findByRuta", query = "SELECT c FROM Carretera c WHERE c.ruta = :ruta"),
    @NamedQuery(name = "Carretera.findByVelocidad", query = "SELECT c FROM Carretera c WHERE c.velocidad = :velocidad"),
    @NamedQuery(name = "Carretera.findByNumcarriles", query = "SELECT c FROM Carretera c WHERE c.numcarriles = :numcarriles")})
public class Carretera extends EntityBean<Carretera> {
    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @Column(name = "id", nullable = false)
    private Integer id;
    @Basic(optional = false)
    @Column(name = "ruta", nullable = false, length = 45)
    private String ruta;
    @Basic(optional = false)
    @Column(name = "velocidad", nullable = false, length = 45)
    private String velocidad;
    @Basic(optional = false)
    @Column(name = "numcarriles", nullable = false, length = 45)
    private String numcarriles;
    @JoinTable(name = "CARRETERA_SERVICIO", joinColumns = {
        @JoinColumn(name = "carretera", referencedColumnName = "id", nullable = false)}, inverseJoinColumns = {
        @JoinColumn(name = "servicio", referencedColumnName = "id", nullable = false)})
    @ManyToMany
    private final ChangeSupportList<Servicio> servicioList = new ChangeSupportList<Servicio>();
    @JoinColumn(name = "tipo", referencedColumnName = "id", nullable = false)
    @ManyToOne(optional = false)
    private TipoCarretera tipoCarretera;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "carretera")
    private final ChangeSupportList<Tramo> tramoList = new ChangeSupportList<Tramo>();

    public Carretera() {
    }

    public Carretera(Integer id) {
        this.id = id;
    }

    public Carretera(Integer id, String ruta, String velocidad, String numcarriles) {
        this.id = id;
        this.ruta = ruta;
        this.velocidad = velocidad;
        this.numcarriles = numcarriles;
    }

    public Carretera(Integer id, String ruta, String velocidad, String numcarriles, List<Servicio> servicioList, TipoCarretera tipoCarretera, List<Tramo> tramoList) {
        this.id = id;
        this.ruta = ruta;
        this.velocidad = velocidad;
        this.numcarriles = numcarriles;
        mergeListContent(this.servicioList, servicioList);
        this.tipoCarretera = tipoCarretera;
        mergeListContent(this.tramoList, tramoList);
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
        support.fireChange();
    }

    public String getRuta() {
        return ruta;
    }

    public void setRuta(String ruta) {
        this.ruta = ruta;
        support.fireChange();
    }

    public String getVelocidad() {
        return velocidad;
    }

    public void setVelocidad(String velocidad) {
        this.velocidad = velocidad;
        support.fireChange();
    }

    public String getNumcarriles() {
        return numcarriles;
    }

    public void setNumcarriles(String numcarriles) {
        this.numcarriles = numcarriles;
        support.fireChange();
    }

    public List<Servicio> getServicioList() {
        return servicioList;
    }

    public void setServicioList(List<Servicio> servicioList) {
        mergeListContent(this.servicioList, servicioList);
//        support.fireChange(); <-- La lista ya controla los cambios
    }

    public TipoCarretera getTipoCarretera() {
        return tipoCarretera;
    }

    public void setTipoCarretera(TipoCarretera tipoCarretera) {
        this.tipoCarretera = tipoCarretera;
        support.fireChange();
    }

    public List<Tramo> getTramoList() {
        return tramoList;
    }

    public void setTramoList(List<Tramo> tramoList) {
        mergeListContent(this.tramoList, tramoList);
//        support.fireChange(); <-- La lista ya controla los cambios
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        if (!(object instanceof Carretera)) return false;
        Carretera other = (Carretera) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return ruta;
    }

    @Override
    public void addChangeListener(ChangeListener listener) {
        servicioList.addChangeListener(listener);
        tramoList.addChangeListener(listener);
        super.addChangeListener(listener);
    }

    @Override
    public Carretera clone() {
        return new Carretera(id, ruta, velocidad, numcarriles, servicioList, tipoCarretera, tramoList);
    }

    @Override
    public void merge(Carretera other) {
        this.id = other.id;
        this.ruta = other.ruta;
        this.velocidad = other.velocidad;
        this.numcarriles = other.numcarriles;
        mergeListContent(this.servicioList, other.servicioList);
        this.tipoCarretera = other.tipoCarretera;
        mergeListContent(this.tramoList, other.tramoList);
    }

}
